package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.widget.LinearLayout;

import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.ui.EaseContactListFragment;
import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.NurseBean;
import com.hxkcloud.nurse.beans.NurseWholeBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by william_sun on 2016-08-17.
 * Description:展示患者所有联系人，即科室所有护士，仅绑定过的可以点击聊天
 */
public class ContactListActivity extends BaseFragmentActivity {

    private LinearLayout ll_title;
    private LinearLayout ll_list;

    private EaseContactListFragment contactListFragment;

    private List<NurseBean> nurses;
    private Map<String, EaseUser> contacts;
    private String bindingNuser;//记录绑定的护士的tel

    @Override
    protected void updateByHandler(Message msg) {
        switch ((msg.what)) {
            case MessageCode.NETWORKCODE_GETDEPARTMENTNURSELIST_SUCCESS:
                if (msg.obj != null) {
                    NurseWholeBean nurseWhole = (NurseWholeBean) msg.obj;
//                    totalCount = nurseWhole.getTotalCount();
                    nurses.addAll(nurseWhole.getList());
                    setContacts();
                } else {
                    toastMsg(R.string.departmentnursesgetfail);
                }
                break;
            case MessageCode.NETWORKCODE_GETDEPARTMENTNURSELIST_FAILURE:
                if (msg.obj != null) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.departmentnursesgetfail);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contactlist);

//        EaseUI.getInstance().init(this, null);
        initView();
    }

    private void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_contactlist_title);
        ll_list = (LinearLayout) findViewById(R.id.ll_contactlist);

        setTitlePadding(ll_title);

        contactListFragment = new EaseContactListFragment();

//        contactListFragment.setContactsMap(contacts);

        contactListFragment.setContactListItemClickListener(new EaseContactListFragment.EaseContactListItemClickListener() {
            @Override
            public void onListItemClicked(EaseUser user) {
                if (user.getUsername().equals(bindingNuser)) {
                    startActivity(new Intent(ContactListActivity.this, ChatFragmentActivity.class).putExtra(EaseConstant.EXTRA_USER_ID,
                            user.getUsername()).putExtra("name", user.getNick().substring(0, 1) + "护士"));
                }
            }
        });

        nurses = new ArrayList<>();
        loading();
        YiJiaHuNetWorkUtils.getNurseListWhole(loginInfo.getDepartmentId(), handler);
//        YiJiaHuNetWorkUtils.getNurseList(loginInfo.getDepartmentId(), 1, 20, handler);
    }

    //设置展示联系人
    private void setContacts() {
        if (nurses != null && nurses.size() > 0) {
            contacts = new HashMap<>();
//            for (NurseBean nurse : nurses) {
//                if (nurse.getIsBinding().equals("0")) {
//                    bindingNuser = "ejh" + nurse.getTel();
//                }
//                EaseUser user = new EaseUser("ejh" + nurse.getTel());
//                user.setAvatar(nurse.getImg());
//                user.setNick(nurse.getName());
//                contacts.put("item", user);
//            }
            for (int i = 0; i < nurses.size(); i++) {
                NurseBean nurse = nurses.get(i);
                if (nurse.getIsBinding().equals("0")) {
                    bindingNuser = "ejh" + nurse.getTel();
                }
                EaseUser user = new EaseUser("ejh" + nurse.getTel());
                user.setAvatar(nurse.getImg());
                user.setNick(nurse.getName());
                contacts.put("item" + i, user);
            }

            contactListFragment.setContactsMap(contacts, bindingNuser);
            getSupportFragmentManager().beginTransaction().add(R.id.ll_contactlist, contactListFragment).commit();
        }
    }
}
